package com.example.TodayNews.fragments;


import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.TodayNews.ChannelInit;
import com.example.TodayNews.R;
import com.example.TodayNews.adapters.ContentFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Intellij IDEA
 * Project: TodayNews
 * Author: magic_chen_
 * Email: 196607376@qq.com
 * Date: 2015/6/16
 */
public class NewsFragment extends Fragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener{
    private RadioGroup radioGroup;
    private ViewPager viewPager;
    private HorizontalScrollView horizontalScrollView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View  view = inflater.inflate(R.layout.fragment_news, container, false);
        radioGroup = (RadioGroup)view.findViewById(R.id.scrollView_radiogroup);
        viewPager = (ViewPager) view.findViewById(R.id.fragment_viewpager);
        horizontalScrollView = (HorizontalScrollView) view.findViewById(R.id.scrollView);
        List<android.support.v4.app.Fragment> fragmentList = new ArrayList<>();
        //初始化频道集合
        List<String> channel = ChannelInit.init();
        //根据频道集合信息，生成RadioGroup 中的RadioButton和ViewPager中的Fragment
        int count = channel.size();
        for(int i = 0;i<count;i++){
            String name = channel.get(i);
            CommonContentFragment fragment = new CommonContentFragment();
            Bundle bundle = new Bundle();
            bundle.putString("channelName",name);
            fragment.setArguments(bundle);
            fragmentList.add(fragment);
            RadioButton radioButton = new RadioButton(getActivity());
            radioButton.setText(name);

            //设置RadioButton被选中时的选择器
            radioButton.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
            Resources resources = getResources();
            ColorStateList colorStateList = resources.getColorStateList(R.color.title_color_selector);
            radioButton.setTextColor(colorStateList);
            radioGroup.addView(radioButton);
            //第一个RadioButton据左侧60,右侧30，最后一个据右侧60，左侧30，其余左右各相距30
            if(i==0){
                radioButton.setPadding(60, 0, 30, 0);
                //第一个RadioButton默认被选中
                radioButton.setChecked(true);
            }else if(i == count-1){
                radioButton.setPadding(30,0,60,0);
            }else{
                radioButton.setPadding(30,0,30,0);
            }
        }
        ContentFragmentAdapter adapter = new ContentFragmentAdapter(getChildFragmentManager(),fragmentList);
        viewPager.setAdapter(adapter);
        //对viewPager设置监听
        viewPager.setOnPageChangeListener(this);
        //对RadioGroup设置监听
        radioGroup.setOnCheckedChangeListener(this);
        return view;
    }
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }
    //ViewPager和
    @Override
    public void onPageSelected(int i) {
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
        radioButton.setChecked(true);
        //todo Radio向左移动，居中RadioButton的位置
        int left = radioButton.getLeft();
        //计算ScrollView 的宽度，获取一半的尺寸
        int l = horizontalScrollView.getLeft();
        int r = horizontalScrollView.getRight();
        int w = r-l;
        w = w/2;
        left = left-w;
        l = radioButton.getLeft();
        r = radioButton.getRight();
        w = r-l;
        w=w/2;
        left+=w;
        horizontalScrollView.smoothScrollTo(left, 0);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
    //RadioGroup和ViewPager的联动
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int count = radioGroup.getChildCount();
        if (count != 0) {
            for (int i = 0; i < count; i++) {
                RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
                boolean isChecked = radioButton.isChecked();
                if(isChecked){
                    viewPager.setCurrentItem(i);
                }
            }
        }
    }
}